# bouncing_lines.py
#
# by Matthew Weathers, to play with gyGame module, adapted from C++ examples in Fall 2005
# CSCI 105 class at Biola University.
# 26-Dec-2009

#Import Modules
import pygame, sys, time, random
from pygame.locals import *
from bgi_colors import * # imports 64 colors in BGIcolors, plus BLACK, BLUE...

#functions

#classes for screen objects

class MovingPoint:
    """A moving point with location and speed, automatically bounces."""
    def __init__(self, maxx=640, maxy=480, initx=0, inity=0,
                 initxspeed=0, inityspeed=0):
        self.x = initx
        self.y = inity
        self.xv = initxspeed # xv stands for "X Velocity"
        self.yv = inityspeed
        self.maxx = maxx
        self.maxy = maxy

    def update(self):
        self.prevx = self.x
        self.prevy = self.y
        self.x += self.xv
        self.y += self.yv
        if (self.x<0 or self.x>self.maxx):
            self.xv *= -1
        if (self.y<0 or self.y>self.maxy):
            self.yv *= -1
        #Sanity check, if they're way off, reset to middle
        if (self.x<-20 or self.y<-20 or self.x>self.maxx+20 or
            self.y>self.maxy+20):
            self.x = self.maxx/2
            self.y = self.maxy/2

def main():
    """this function is called when the program starts.
       it initializes everything it needs, then runs in
       a loop until the function returns."""
# Initialize pygame, window
    pygame.init()
    mainClock = pygame.time.Clock()
    WINDOWWIDTH = 800
    WINDOWHEIGHT = 600
    windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
    pygame.display.set_caption('Bouncing Lines - MDW')

# Fill screen with a color
    windowSurface.fill(LIGHTBLUE)

# Write a rectangle of text
    basicFont = pygame.font.SysFont(None, 48)
    text = basicFont.render('Bouncing Lines', True, WHITE, BLUE)
    textRect = text.get_rect()
    textRect.centerx = windowSurface.get_rect().centerx
    textRect.centery = windowSurface.get_rect().centery
    windowSurface.blit(text, textRect)
    pygame.display.update()

# Wait for a mouseclick down, then continue
    print str(pygame.event.get()) # Toss all current events
    while pygame.event.get(MOUSEBUTTONDOWN) == []:
        time.sleep(0.2)

# Fill screen with a color
    windowSurface.fill(BLACK)
    pygame.display.update()

# Prepare Objects
    boxes = []
    for i in range(20):
        boxes.append(MovingPoint(WINDOWWIDTH,WINDOWHEIGHT,
                                 random.randint(0,WINDOWWIDTH-10),
                                 random.randint(0,WINDOWHEIGHT-10),
                                 random.uniform(-3.0, 3.0),
                                 random.uniform(-3.0, 3.0)))

# Initial Settings
    gravityOn = False
    tailsOn = False

# MAIN GAME LOOP
    while True:
        mainClock.tick(100) # Never run more than 20 FPS

    #Handle Input Events
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYUP:
                if event.key == K_ESCAPE:
                    return
                if event.key == ord('g'):
                    gravityOn = not gravityOn
                if event.key == ord('t'):
                    tailsOn = not tailsOn
                if event.key == ord('c'):
                    windowSurface.fill(BLACK)
    #Update objects
        for i in range(len(boxes)):
            boxes[i].update()
            if gravityOn:
                boxes[i].yv += 0.1
            if (not tailsOn):
                pygame.draw.rect(windowSurface, BLACK,
                                 pygame.Rect(boxes[i].prevx-5,boxes[i].prevy-5,
                                             10,10))
            pygame.draw.rect(windowSurface, BGIcolors[i+1],
                             pygame.Rect(boxes[i].x-5,boxes[i].y-5,
                                         10,10))
        pygame.display.update()
        

        
    # END GAME LOOP


#Game Over, end of main()

#this calls the 'main' function when this script is executed
if __name__ == '__main__':
    main()
    #Quit everything
    pygame.quit()
    sys.exit()

